package com.twu.biblioteca;

import com.twu.biblioteca.model.Account;
import com.twu.biblioteca.service.BookService;
import com.twu.biblioteca.service.IService;
import com.twu.biblioteca.service.MovieService;
import com.twu.biblioteca.service.UserService;
import com.twu.biblioteca.util.CommonUtils;

public class MainMenu {
    Library library = new Library();
    IService bookService = new BookService();
    IService movieService = new MovieService();
    UserService userService = new UserService();
    private Account account = null;

    public void welcomeTo() {
        System.out.println("*******************************************************************************");
        System.out.println("                           Welcome to Biblitoteca Library!                 ");
        bookService.printAll();
    }

    public void libraryOption() {
        library.mainMenu();
        String inputStr = CommonUtils.getInput();
        if (inputStr.equalsIgnoreCase("ListBooks")) {
            bookService.printAll();
            bookService.menu();
            bookOption();
        } else if (inputStr.equalsIgnoreCase("ListMovies")) {
            movieService.printAll();
            movieService.menu();
            movieOption();
        } else if (inputStr.equalsIgnoreCase("UserAccounts")) {
            userService.menu();
            accountOption();
        } else if (inputStr.equalsIgnoreCase("Quit")) {
            CommonUtils.logoutSystem();
        } else if (inputStr.equalsIgnoreCase("Login")) {
            printLogin();
        } else {
            System.out.println("Select a valid option!");
        }
        libraryOption();
    }

    private void movieOption() {
        String inputStr = CommonUtils.getInput();
        if (inputStr.equalsIgnoreCase("CheckoutMovie")) {
            if (checkLogin()) {
                System.out.print("Please input the movieId:");
                int movieId = CommonUtils.intConversion(CommonUtils.getInput());
                System.out.println(movieService.checkout(movieId, library.movieList));
            } else {
                System.out.println("Please login first!(You can input command 'login' to login the System.)");
            }
        } else if (inputStr.equalsIgnoreCase("ReturnMovie")) {
            if (checkLogin()) {
                System.out.print("Please input the movieId:");
                int movieId = CommonUtils.intConversion(CommonUtils.getInput());
                System.out.println(movieService.revert(movieId, library.movieList));
            } else {
                System.out.println("Please login first!(You can input command 'login' to login the System.)");
            }
        } else if (inputStr.equalsIgnoreCase("BackMainmenu")) {
            libraryOption();

        } else if (inputStr.equalsIgnoreCase("Quit")) {
            CommonUtils.logoutSystem();
        } else if (inputStr.equalsIgnoreCase("Login")) {
            printLogin();
        } else {
            System.out.println("Select a valid option!");
        }
        movieService.printAll();
        movieService.menu();
        movieOption();
    }

    public void bookOption() {
        String inputStr = CommonUtils.getInput();
        if (inputStr.equalsIgnoreCase("CheckoutBook")) {
            if (checkLogin()) {
                System.out.print("Please input the bookId:");
                int bookId = CommonUtils.intConversion(CommonUtils.getInput());
                System.out.println(bookService.checkout(bookId, library.bookList));
            } else {
                System.out.println("Please login first!(You can input command 'login' to login the System.)");
            }

        } else if (inputStr.equalsIgnoreCase("ReturnBook")) {
            if (checkLogin()) {
                System.out.print("Please input the bookId:");
                int bookId = CommonUtils.intConversion(CommonUtils.getInput());
                System.out.println(bookService.revert(bookId, library.bookList));
            } else {
                System.out.println("Please login first!(You can input command 'login' to login the System.)");
            }
        } else if (inputStr.equalsIgnoreCase("BackMainmenu")) {
            libraryOption();
        } else if (inputStr.equalsIgnoreCase("Quit")) {
            CommonUtils.logoutSystem();
        } else if (inputStr.equalsIgnoreCase("Login")) {
            printLogin();
        } else {
            System.out.println("Select a valid option!");
        }
        bookService.printAll();
        bookService.menu();
        bookOption();
    }

    public void accountOption() {
        String inputStr = CommonUtils.getInput();
        if (inputStr.equalsIgnoreCase("Login")) {
            printLogin();
        } else if (inputStr.equalsIgnoreCase("UserInformation")) {
            if (checkLogin()) {
                userService.printAccountInformation(account);
            } else {
                System.out.println("Please login first!(You can input command 'login' to login the System.)");
            }
        } else if (inputStr.equalsIgnoreCase("BackMainmenu")) {
            libraryOption();
        } else if (inputStr.equalsIgnoreCase("Quit")) {
            CommonUtils.logoutSystem();
        } else {
            System.out.println("Select a valid option!");
        }
        userService.menu();
        accountOption();

    }

    public boolean checkLogin() {
        boolean loginFlag = false;
        if (account != null) {
            loginFlag = true;
        }
        return loginFlag;
    }


    public void printLogin() {
        if (!checkLogin()) {
            System.out.print("Please input the accountId:");
            String accountId = CommonUtils.getInput();
            System.out.print("Please input the accountPassword:");
            String password = CommonUtils.getInput();
            if (userService.userLogin(accountId, password)) {
                account = userService.queryById(accountId);
                System.out.println("Login Success!");
            } else {
                System.out.println("AccounId or Password wrong,Please try again!");
            }
        } else {
            System.out.println("You have login！");
        }


    }
}
